<?php

namespace App\Http\Requests\Personal\Login;

use App\Http\Requests\Request;
use App\Models\Personal\PersonalTrialError;
use App\Models\Personal\PersonalUser;
use Validator;


class AjaxLoginRequest extends Request{
    public function authorize(){
        return true;
    }

    public function rules(){

        //自定义手机验证规则改写
        Validator::extend('isset_name', function($attribute, $value, $parameters){
            $personalUser = PersonalUser::where('username', $value)->orWhere('mobile', $value)->get();
            if(count($personalUser)){
                return true;
            }else{
                //试错记录表
                $trial = array();
                $trial['username'] = $this->username;
                $trial['password'] = $this->password;
                $trial['personal_login_ip'] = getIp();
                //登录记录表
                PersonalTrialError::create($trial);
                return false;
            }
        });
        return [
            'username' => 'required|isset_name',
            'password' => 'required'
        ];
    }

    public function messages(){
        return [
            'username.required' => '用户名必须填写',
            'username.isset_name' => '用户名不存在',
            'password.required' => '密码必须填写'
        ];
    }
}
